package com.leon.androidcoursedesign.ui.activitys;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.graphics.Typeface;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.leon.androidcoursedesign.R;
import com.leon.androidcoursedesign.adapters.ViewPageAdapter;
import com.leon.androidcoursedesign.ui.dialogs.LogoutDialog;
import com.leon.androidcoursedesign.ui.fragments.mainFragments.CourseFragment;
import com.leon.androidcoursedesign.ui.fragments.mainFragments.ToolBoxFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HomeActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    private static final int TimeInterval = 2000;
    private long backPressedTime;

    private TextView
            home_textView_course,
            home_textView_toolBox;
    private ViewPager home_viewPager;

    private Map<Integer, TextView> textViewMap;
    private LogoutDialog logoutDialog;

    @Override
    public int setLayout() {
        return R.layout.activity_home;
    }

    @Override
    public void initView() {
        home_textView_course = findViewById(R.id.home_textView_course);
        home_textView_course.setOnClickListener(this);
        home_textView_toolBox = findViewById(R.id.home_textView_toolBox);
        home_textView_toolBox.setOnClickListener(this);

        home_viewPager = findViewById(R.id.home_viewPager);

        this.setOnClick(R.id.home_main_logout);
    }

    @Override
    public void initValue() {
        textViewMap = new HashMap<>();
        textViewMap.put(0, home_textView_course);
        textViewMap.put(1, home_textView_toolBox);

        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new CourseFragment());
        fragments.add(new ToolBoxFragment());

        ViewPageAdapter viewPageAdapter = new ViewPageAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT, fragments);
        home_viewPager.setAdapter(viewPageAdapter);
        home_viewPager.setOffscreenPageLimit(2);
        home_viewPager.addOnPageChangeListener(this);
    }

    @Override
    public void onBackPressed() {
        if (backPressedTime + TimeInterval > System.currentTimeMillis()) {
            super.onBackPressed();
        } else {
            Toast.makeText(this, "再点击一次退出该程序", Toast.LENGTH_SHORT).show();
            backPressedTime = System.currentTimeMillis();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_main_logout:
                //创建弹窗
                if (logoutDialog == null) {
                    logoutDialog = new LogoutDialog(HomeActivity.this);
                    logoutDialog.setOnLogoutDialogDismissListener(new LogoutDialog.OnLogoutDialogDismissListener() {
                        @Override
                        public void onDismiss() {
                            finish();
                        }
                    });
                }

                logoutDialog.show();
                break;
            case R.id.home_textView_course:
                home_viewPager.setCurrentItem(0);
                break;
            case R.id.home_textView_toolBox:
                home_viewPager.setCurrentItem(1);
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageSelected(int position) {
        changeText(textViewMap, position);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    /**
     * 切换Fragment时，顶部文字的变化
     *
     * @param textViewMap   TextView
     * @param position  position
     */
    private void changeText(Map<Integer, TextView> textViewMap, int position) {
        int pointRoundBlueLightBg = R.drawable.round_blue_light_bg;
        int pointRippleBlueBg = R.drawable.ripple_blue_bg;

        Typeface typeface_bold = Typeface.defaultFromStyle(Typeface.BOLD);
        Typeface typeface_normal = Typeface.defaultFromStyle(Typeface.NORMAL);

        TextView textView;
        for (Map.Entry<Integer, TextView> entry : textViewMap.entrySet()) {
            if (entry.getKey() == position) {
                textView = entry.getValue();
                textView.setBackgroundResource(pointRoundBlueLightBg);
                textView.setTextSize(18);
                textView.setTypeface(typeface_bold);
            } else {
                textView = entry.getValue();
                textView.setBackgroundResource(pointRippleBlueBg);
                textView.setTextSize(15);
                textView.setTypeface(typeface_normal);
            }
        }
    }
}